package sd.qd.xiaoming.coffeedesk.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import sd.qd.xiaoming.coffeedesk.dao.CdDirectoryDAO;
import sd.qd.xiaoming.coffeedesk.dao.CdFileDAO;
import sd.qd.xiaoming.coffeedesk.entity.CdFile;
import sd.qd.xiaoming.coffeedesk.support.FileSupport;

@Transactional
@Service
public class CdFileService extends BaseService<CdFileDAO, CdFile> {

	@Autowired
	private CdFileDAO dao;
    @Autowired
    private CdDirectoryDAO cdDirectoryDAO;
    @Autowired
    private FileSupport fileSupport;

    public boolean deleteById(final Long id) {
        try {
            CdFile cdFile = this.dao.findById(id);
            this.fileSupport.delete(cdFile.getCode());
            this.dao.delete(cdFile);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public List<CdFile> findByDirectoryId(final Long did) {
        return this.dao.findByDirectory(this.cdDirectoryDAO.findById(did));
    }

}
